package com.pine.dcts.utils;




import com.pine.dcts.model.ITreeEntity;

import java.util.ArrayList;
import java.util.List;

/**
 *  树形结构实体的工具类
 * @author xiaoyuan
 * @create 2020/6/15 9:51
 **/
public class TreeUtils {


    /**
     * * 解析树形数据
     *
     * @param entityList
     * @return
     */
    public static <E extends ITreeEntity<E>> List<E> getTreeList(List<E> entityList) {
        List<E> resultList = new ArrayList<>();
        // 获取顶层元素集合
        Integer parentId;
        for (E entity : entityList) {
            parentId = entity.getParentId();
            if (parentId==null ) {
                resultList.add(entity);
            }
        }
        // 获取每个顶层元素的子数据集合
        for (E entity : resultList) {
            entity.setChildren(getSubList(entity.getId(), entityList));
        }
        return resultList;
    }

    /**
     * * 解析树形数据
     *
     * @param topId
     * @param entityList
     * @return
     */
    public static <E extends ITreeEntity<E>> List<E> getTreeList(Integer topId, List<E> entityList) {
        List<E> resultList = new ArrayList<>();
        // 获取顶层元素集合
        Integer parentId;
        for (E entity : entityList) {
            parentId = entity.getParentId();
            if(topId==null){
                if (parentId==null) {
                    resultList.add(entity);
                }
            }else{
                if (topId != null && topId.equals(parentId)) {
                    resultList.add(entity);
                }
            }

        }
        // 获取每个顶层元素的子数据集合
        for (E entity : resultList) {
            entity.setChildren(getSubList(entity.getId(), entityList));
        }
        return resultList;
    }

    /**
     * * 获取子数据集合
     *
     * @param id
     * @param entityList
     * @return
     */
    private static <E extends ITreeEntity<E>> List<E> getSubList(Integer id, List<E> entityList) {
        List<E> childList = new ArrayList<>();
        Integer parentId;
        // 子集的直接子对象
        for (E entity : entityList) {
            parentId = entity.getParentId();
            if (id.equals(parentId)) {
                childList.add(entity);
            }
        }
        // 子集的间接子对象
        for (E entity : childList) {
            entity.setChildren(getSubList(entity.getId(), entityList));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
